package de.hsowl.tod4a.global.xml;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;

import de.hsowl.tod4a.server.ObjectServerImpl;

public class ElementHelper {

	public Element element;
	public DataHelper data = null;

	public ElementHelper(Element e) {
		this.element = e;
		data = new DataHelper(ObjectServerImpl.prop);
	}

	public String getText() {
		String text = element.getText();
		text = data.restoreString(text);
		return text;
	}

	public String getName() {
		String text = element.getName();
		text = data.restoreString(text);
		return text;
	}

	public String getAttributeValue(String name) {
		try {
			String text = element.getAttributeValue(name);
			if (text != null) {
				text = data.restoreString(text);
				return text;
			}
		} catch (Exception e) {
		}
		return null;
	}

	public void setAttribute(String name, String value) {
		value = data.prepareString(value);
		element.setAttribute(name, value);
	}

	public void setText(String text) {
		text = data.prepareString(text);
		element.setText(text);

	}

	public void setName(String name) {
		name = data.prepareString(name);
		element.setName(name);
	}

	@SuppressWarnings("unchecked")
	public List<Element> getChildren() {
		return (List<Element>) element.getChildren();
	}

	public List<ElementHelper> getChildrenAsHelper() {
		ArrayList<ElementHelper> list = new ArrayList<ElementHelper>();
		for (Object o : element.getChildren()) {
			list.add(new ElementHelper((Element) o));
		}
		return list;
	}

	public Element getElement() {
		return element;
	}

	public void addContent(Element child) {
		element.addContent(child);
	}

	public void addContent(ElementHelper eh) {
		addContent(eh.element);
	}

	public ElementHelper getChild(String name) {
		Element e = element.getChild(name);
		if (e == null) {
			return null;
		} else {
			return new ElementHelper(e);
		}
	}
}
